/* ====================================================================
 * (#)Header: EntityServiceImpl.java
 * (#)Revision: 
 * (#)Date Created: 2009-02-19
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.service.entity;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.karolak.dao.Dao;
import com.karolak.query.Criterias;
import com.karolak.service.entity.EntityService;

/**
 * 
 * <code>EntityServiceImpl</code> is ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class EntityServiceImpl<T, Id> implements EntityService<T, Id> {

	protected final Log log = LogFactory.getLog(getClass());

	private Class<T> persistentClass;

	private Dao dao;

	public EntityServiceImpl(Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}

	// === dao delegates === //
	// === finders === //
	public T findById(Id id) {
		return getDao().findById(getPersistentClass(), id);
	}

	public T find(Criterias criterias) {
		return getDao().find(getPersistentClass(), criterias);
	}

	// === crud === //
	public T create(T entity) {
		entity = getDao().persist(entity);
		return entity;
	}

	public T update(T entity) {
		entity = getDao().merge(entity);
		return entity;
	}

	public void remove(Id id) {
		getDao().remove(getPersistentClass(), id);
	}

	// === selectors === //
	public List<T> select() {
		return getDao().select(getPersistentClass());
	}

	public List<T> select(Criterias criterias) {
		return getDao().select(getPersistentClass(), criterias);
	}

	// === range selectors === //
	public List<T> select(int startPosition, int maxResult) {
		return getDao().select(getPersistentClass(), startPosition, maxResult);
	}

	public List<T> select(Criterias criterias, int startPosition, int maxResult) {
		return getDao().select(getPersistentClass(), criterias, startPosition, maxResult);
	}

	// === count selectors === //
	public int selectCount() {
		return getDao().selectCount(getPersistentClass());
	}

	public int selectCount(Criterias criterias) {
		return getDao().selectCount(getPersistentClass(), criterias);
	}

	// === get/set === //
	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

	public Class<T> getPersistentClass() {
		return persistentClass;
	}

	public void setPersistentClass(Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}
}
